﻿using CompanyManageSystem.DLL;
using CompanyManageSystem.Model;
using CompanyManageSystem.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CompanyManageSystem.UI.Info
{
    public partial class Frm_AddSupplier : BaseXtraForm
    {
        #region 属性
        public EStatus FStatus = EStatus.Add;
        public Guid ID { get; set; }
        public event EventHandler SaveEvent ;
        #endregion

        /// <summary>
        /// 构造方法
        /// </summary>
        public Frm_AddSupplier(Guid id,EStatus status)
        {
            InitializeComponent();
            this.ID = id;
            this.FStatus = status;
        }

        /// <summary>
        /// 窗体加载完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Frm_AddSupplier_Load(object sender, EventArgs e)
        {
            if (FStatus == EStatus.Add)
            {
                this.Text = "供应商 - 添加";
            }
            if (FStatus == EStatus.Modify)
            {
                RepastService aservice = new RepastService();
                Supplier supplier = aservice.GetSupplierByID(this.ID);
                this.Text = "供应商 - 编辑";
                EditCode.Text = supplier.Code;
                EditName.Text = supplier.Name;
                EditAddress.Text = supplier.Address;
                EditContracts.Text = supplier.Contracts;
                EditPhone.Text = supplier.Phone;
            }
        }

        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_OK_Click(object sender, EventArgs e)
        {
            RepastService aservice = new RepastService();
            Supplier supplier = null;
            if (FStatus == EStatus.Add)
            {
                supplier = aservice.AddSupplier(ID, this.EditCode.Text.Trim(), this.EditName.Text.Trim(), this.EditContracts.Text.Trim(), this.EditPhone.Text.Trim(), this.EditAddress.Text.Trim());
                this.Hide();
                MessageBox.Show("添加成功!");
            }
            if (FStatus == EStatus.Modify)
            {
                supplier = aservice.UpdateSupplier(ID, this.EditCode.Text.Trim(), this.EditName.Text.Trim(), this.EditContracts.Text.Trim(), this.EditPhone.Text.Trim(), this.EditAddress.Text.Trim());
                this.Hide();
                MessageBox.Show("编辑成功");
            }
            if (SaveEvent != null)
            {
                SaveEvent(supplier, new EventArgs());
            }
            this.Close();
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_Cancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
